'''
Created on Dec 9, 2011

@author: conan
'''
import csv

#Master file labels:
#CRN, stduentID, course name, title, instructor, exam date, location
def write_jobs(filename):
    job_dict = dict()
    writer = csv.writer(open('jobs.csv', 'wb'))
    reader = csv.reader(open(filename))
    labels = reader.next()
    writer.writerow(labels[0:1] + labels[2:5] + ['PT', 'REQ', 'PREC'])
    
    for row in reader:
        crn, attribs, student = int(row[0]), tuple(row[2:5] + [1, 0, None]), row[1]
        if crn not in job_dict:
            job_dict[crn] = (crn,) + attribs + ([student],)
            
        else:
            job_dict[crn][-1].append(student)
    
    for crn in sorted(job_dict.keys()):
#        print job_dict[crn], len(job_dict[crn][-1])
        writer.writerow(job_dict[crn])
    
    return job_dict

def write_matrix(job_dict):   
    writer = csv.writer(open('matrix.csv', 'wb')) 
    #row=crn1
    for crn1 in sorted(job_dict.keys()):
        #column=crn2
        row = []
        for crn2 in sorted(job_dict.keys()):
            if crn1 == crn2: 
                row.append(0)
            else:
                student_list1 = job_dict[crn1][-1]
                student_list2 = job_dict[crn2][-1]
                same_students = set(student_list1) & set(student_list2)
                row.append(len(same_students))
        writer.writerow(row)

            
        





if __name__ == '__main__':
    jd = write_jobs('masterFile.csv')
    write_matrix(jd)
